package com.oa.ylms.apps.dao.archives.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.archives.IArchiveInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;

public class ArchiveInfoDaoImpl extends HibernateDaoSupport implements
		IArchiveInfoDao {

	@Override
	public int addArchiveInfo(ArchiveInfoValue archiveInfo) throws Exception {
		if(archiveInfo != null){
			this.getHibernateTemplate().save(archiveInfo);
			return archiveInfo.getArchiveId();
		}
		return 0;
	}

	@Override
	public boolean editArchiveInfo(ArchiveInfoValue archiveInfo)
			throws Exception {
		if(archiveInfo != null){
			try{
				this.getHibernateTemplate().update(archiveInfo);
			}catch(Exception e){
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public ArchiveInfoValue queryByArchiveId(ArchiveInfoValue archiveInfo)
			throws Exception {
		if(archiveInfo != null){
			return (ArchiveInfoValue) this.getHibernateTemplate().get(ArchiveInfoValue.class, archiveInfo.getArchiveId());
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ArchiveInfoValue> queryArchiveNo(ArchiveInfoValue archiveInfo) throws Exception{
		String hql = "FROM ArchiveInfoValue aiv WHERE aiv.archiveNo = (SELECT max(aiv1.archiveNo) FROM ArchiveInfoValue aiv1 WHERE aiv1.archiveNo like ?)";
		return this.getHibernateTemplate().find(hql,archiveInfo.getArchiveNo());
	}

}
